package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.ContractMapper;
import com.unhz.generator.dao.ProductionOrderInfoMapper;
import com.unhz.generator.dao.ProductionOrderMapper;
import com.unhz.generator.dao.SysRolePermissionMapper;
import com.unhz.generator.enums.EnumProductionOrder;
import com.unhz.generator.pojo.Contract;
import com.unhz.generator.pojo.ProductionOrder;
import com.unhz.generator.pojo.ProductionOrderInfo;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.taigu.service.ProductionOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.awt.EmbeddedFrame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹显贵
 * @Description:
 * @Date: Created in 10:19 2018/1/30 0030
 * @Modified By:
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    static final int PAGENUM = 1;
    private Logger logger = LoggerFactory.getLogger(ProductionOrderServiceImpl.class);

    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private ProductionOrderInfoMapper productionOrderInfoMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public Page<ProductionOrder> findPageAllProductionOrder(String pageNo, Integer pageSize, String keywords,Integer adminId,Integer roleId) {
        //分页查询
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        //查询roleId中是否存在permissionId对应值的权限
        int count = sysRolePermissionMapper.selectPermissionIdByRoleIdOrder(roleId);
        //获取记录总条数
        int totalRecords = 0;
        if(1==count){
            totalRecords = productionOrderMapper.getTotalRecordsByKeywords("%"+keywords+"%");        //获取记录总条数
        }else{
            totalRecords = productionOrderMapper.getTotalRecordsByKeywordsByAdminId(adminId,keywords);
        }
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords","%"+keywords+"%");
        map.put("adminId",adminId);
        //获取总记录数
        List<ProductionOrder> records = new ArrayList<ProductionOrder>();
        if(1==count){
            records = productionOrderMapper.findProdutionOrderMap(map);      //获取记录总条数
        }else{
            records = productionOrderMapper.findProdutionOrderByAdminId(map);
        }
        //存入集合
        for(ProductionOrder productionOrder:records){
            List<ProductionOrderInfo> list = productionOrderInfoMapper.selectByOrderId(Long.valueOf(productionOrder.getId()));
            if(!EmptyUtil.isEmpty(list)){
                productionOrder.setProductionOrderInfoList(list);
            }
            logger.info("============"+list.size());
        }
        page.setRecords(records);
        String url = "/productionOrder/findAllProductionOrder?keywords="+keywords+"&roleId="+roleId+"&adminId="+adminId;
        page.setUri(url);
        return page;
    }

    @Override
    public ProductionOrder selectByProductOrderId(Integer productOrderId) {

        ProductionOrder productionOrder = productionOrderMapper.selectByPrimaryKey(productOrderId);
        List<ProductionOrderInfo> list = productionOrderInfoMapper.selectByOrderId(Long.valueOf(productOrderId));
        if(!EmptyUtil.isEmpty(list)){
            productionOrder.setProductionOrderInfoList(list);
        }
        List<Contract> contracts = contractMapper.selectByOrderId(Integer.parseInt(productionOrder.getOrderId().toString()));
        if(!EmptyUtil.isEmpty(contracts)){
            productionOrder.setContractList(contracts);
        }
        return productionOrder;
    }

    @Override
    public int deleteByProducOrderId(Integer productOrderId) {
        //先删除数量,再删除订单
        int count = productionOrderInfoMapper.deleteByProductOrderId(productOrderId);
        if(count > 0){
            logger.info("删除生产订单成功!");
        }
        return  productionOrderMapper.deleteByPrimaryKey(Long.valueOf(productOrderId));
    }

    @Override
    public String confirmProductOrder(Integer productOrderId) {
        //修改订单状态
        ProductionOrder productionOrder = productionOrderMapper.selectByPrimaryKey(productOrderId);
        productionOrder.setStatus((byte) EnumProductionOrder.WAIT_PRODUCTION.getIndex());
        int count = productionOrderMapper.updateByPrimaryKeySelective(productionOrder);
        if(count > 0){
            return "success";
        }
        return "failed";
    }

    @Override
    public String carryOutProduct(Integer productOrderId) {
        //修改订单状态
        ProductionOrder productionOrder = productionOrderMapper.selectByPrimaryKey(productOrderId);
        productionOrder.setStatus((byte) EnumProductionOrder.PRODUCTIONING.getIndex());
        int count = productionOrderMapper.updateByPrimaryKeySelective(productionOrder);
        if(count > 0){
            return "success";
        }
        return "failed";
    }

    @Override
    public ProductionOrder selectById(Integer productOrderId) {
        System.out.println("--------------"+productOrderId);
        return productionOrderMapper.selectByPrimaryKey(productOrderId);
    }

    @Override
    public List<ProductionOrder> selectByOrderId(Long orderId) {
        return productionOrderMapper.selectByOrderId(orderId);
    }

    @Override
    public ProductionOrder selectNewProductionOrder(Long orderId) {
        return productionOrderMapper.selectNewProductionOrder(orderId);
    }

    @Override
    public int insertProductionOrder(ProductionOrder productionOrder) {
        return productionOrderMapper.insertSelective(productionOrder);
    }


    //新增
    @Override
    public int insertSelective(ProductionOrder productionOrder) {
        return productionOrderMapper.insertSelective(productionOrder);
    }

    @Override
    public int updateProductionOrder(ProductionOrder productionOrder) {
        return productionOrderMapper.updateByPrimaryKeySelective(productionOrder);
    }

    @Override
    public int deleteProductionOrder(Long productionId) {

        return productionOrderMapper.deleteByPrimaryKey(productionId);
    }


}
